<template>
    <div class="cmt_container">
        <h3 class="cmt_title">发表评论</h3>
        <textarea placeholder="输入你要发表的BB(不能超过120个字)" max-length="120" v-model="msg" ></textarea>
        <mt-button type="primary" size="large" @click="postcomment">发表评论</mt-button>
        <ul class="cmt_list">
            <li class="cmt_item"  v-for="(item,index) in commentList" :key="item.id">
                <p class="cmt_item_tit">
                    第{{ index+1 }}楼&nbsp;&nbsp;用户名：{{item.user_name}}&nbsp;&nbsp;发表时间：{{ item.add_time | dateFormat }}
                </p>
                <p class="cmt_item_con">
                    {{ item.content === "undefined" ? "此用户很懒，什么都没留下" : item.content }}
                </p>
            </li>            
        </ul>
        <mt-button type="danger" size="large" plain @click="loaderMore">加载更多</mt-button>
    </div>
</template>
<script>
import { Toast } from 'mint-ui'
export default {
    data(){
        return{
            pageindex:1,
            commentList:[],
            msg:""
        }
    },
    created() {
        this.getcomment()
    },
    methods: {
        getcomment(){
            this.$http.get("api/getcomments/"+ this.id +"?pageindex="+ this.pageindex).then(res=>{
                if(res.body.status === 0){
                    // console.log(res)
                    // 如果把获取的结果直接赋值给 commetList 在点击加载更多的时候 会把上一页的评论数据覆盖 所以使用 contact 
                    //把获取到的数据连接器
                    this.commentList = this.commentList.concat( res.body.message )
                }else{
                    Toast("获取数据失败...")
                }
            })
        },
        loaderMore(){
            this.pageindex ++
            this.getcomment()
        },
        postcomment(){
            if(this.msg.trim().length == 0){
                return Toast("评论内容不能为空")
            }
            this.$http.post("api/postcomment/"+this.id,{content:this.msg.trim() }).then(res=>{
                if(res.body.status === 0){
                    var cmt = {
                        user_name:"匿名用户",
                        add_time:  Date.now(),
                        content : this.msg.trim()
                    }
                    this.commentList.unshift(cmt)
                    this.msg = ""
                }else{
                    Toast("失败了...")
                }
            })
        }
    },
    props:["id"]//子组件接受父组件传值
}
</script>
<style lang="scss" scoped>
    .cmt_container{
        .cmt_title{
            font-size: 18px;
            padding-bottom: 8px;
            border-bottom: 1px solid #999;
        }
        textarea{
            font-size: 12px;
            height: 85px;
            margin: 0;
        }
        .mint-button{
            font-size: 14px;
        }
        .cmt_list{
            padding: 0;
            margin: 10px 0;
            .cmt_item{
                list-style: none;
                p{
                    margin: 0;
                    line-height: 35px;
                }
                .cmt_item_tit{
                    text-indent: 5px;
                    background-color: #ccc;
                    font-size: 13px;
                }
                .cmt_item_con{
                    text-indent: 2em;
                }
            }
        }
    }
</style>
